<?php

if(!defined('PATH')) exit();

class Install extends Controller {

	protected $default_action = 'start';

	public $_title = 'Install';

	protected $_config = array(
		'enable' => false,
		);

	public function after() {
		parent::after();
	}

	public function before() {
		parent::before();
		if (!$this->_config['enable']) {
			$this->redirect(PATH.'/'.$this->config['website']['index']);
		}
	}

	public function __construct() {
		parent::__construct();
		$this->_config['enable'] = $this->config['global']['install'];
		$this->_folder_template = 'install';
	}

	public function action_start() {
		$this->redirect(PATH.'/install/form');
	}

	public function action_form() {
		$view = new View('install/form');
		$view->render();
	}

	public function action_check() {
		if ($_POST) {
		$name = @$_POST['archez_name'];
		$port = @$_POST['archez_port'];
		$host = @$_POST['archez_host'];
		$user = @$_POST['archez_user'];
		$password = @$_POST['archez_password'];
		$database = @$_POST['archez_database'];
		$publickey = @$_POST['archez_publickey'];
		$privatekey = @$_POST['archez_privatekey'];
		$real_path = @$_POST['archez_real_path'];
		$encrypt = @$_POST['archez_encrypt'];
		$salt = @$_POST['archez_salt'];
		$path = '';
		if (strlen(PATH) > 1)
			$path = substr(PATH, 1);

		if (!$host or !$name or !$user or !$database or !$port or !$privatekey or !$publickey or !$real_path or !$encrypt) {
			echo 'Sorry, you must fill in all the fields, <a href="'.PATH.'/install/start/">click here</a> to go back.';
		} elseif (!$password) {
			echo 'Sorry, but due to security measures the system does not allow databases without password, <a href="'.PATH.'/install/start/">click here</a> to go back.';
		} elseif (@!mysql_connect($host, $user, $password)) {
			echo 'Sorry, but the system was unable to connect to your database, <a href="'.PATH.'/install/start/">click here</a> to go back.';
		} elseif (@!mysql_select_db($database)) {
			echo 'Sorry, but the system was unable to recognize your database, <a href="'.PATH.'/install/start/">click here</a> to go back.';
		} else {
			$replace = @file_get_contents('system/lib/install/config.php') or die('Unable to read config.php!');
			$replace = preg_replace(
				array('/%NAME%/', '/%PATH%/', '/%HOST%/', '/%USER%/', '/%PASSWORD%/', '/%DATABASE%/', '/%PORT%/', '/%PUBLICKEY%/', '/%PRIVATEKEY%/', '/%REALPATH%/', '/%ENCRYPT%/', '/%SALT%/'),
				array($name, $path, $host, $user, $password, $database, $port, $publickey, $privatekey, $real_path, $encrypt, $salt),
				$replace);
			$finish = @file_put_contents('config.php', $replace) or die('Unable to read config.php!');
			mysql_connect($host, $user, $password);
			mysql_select_db($database);
			$ots = POT::getInstance();
			$ots->connect(POT::DB_MYSQL, array('host' => $host, 'user' => $user, 'database' => $database, 'password' => $password));
			$SQL = $ots->getDBHandle();
			$queries = file_get_contents('system/lib/install/database.txt');
			$SQL->query($queries);
			echo 'You have successfuly installed your <b>Archez AAC 0.1.0</b> copy.<br /><br />You can now enjoy your new website by <a href="' . PATH . '/">clicking here.</a>';
		}
		}
	}
}
?>